// JavaScript Document

/** Prijzen **/
var prijsFilm = 8;
var prijsDag = 65;
var prijsCat = 55;
var prijsFull = 170;
var korting = 10;

function laadPrijzen()
{
	/** Prijzen invullen op de pagina **/
	document.getElementById("korting").innerHTML = korting;
	document.getElementById("prijsFilm").innerHTML = prijsFilm;	
	document.getElementById("prijsDag").innerHTML = prijsDag;
	document.getElementById("prijsCat").innerHTML = prijsCat;
	document.getElementById("prijsFull").innerHTML = prijsFull;

}

function resetFormulier()
{
	/** Bij reset eventuele foutboodschappen verwijderen, berekening op 0 zetten, 
	Submit knop terug disablen en textarea niet verplicht maken  **/
	$(".error").slideUp();
	$("#afrekening").slideUp();
	$(".adres").slideUp();
	
	document.getElementById("subtotaal").innerHTML = 0;
	document.getElementById("kortingen").innerHTML = 0;
	document.getElementById("totaal").innerHTML = 0;
	
	document.getElementsByTagName("button").item(0).disabled = true;
	document.getElementsByTagName("button").item(0).className = "disabled";
	
	setVerplicht(false);
}

function updateAfrekening()
{
	/** Gaat alle veldjes af om te controleren op (correcte) invoer, berekent en output het (sub)totaal en eventuele kortingen **/
	var subtotaal = 0;
	var aantalTickets = 0;
	var error = false;
	
	var movie_tickets = parseInt(document.getElementById("movie_tickets").value);
	var friday_tickets = parseInt(document.getElementById("friday_tickets").value);
	var saturday_tickets = parseInt(document.getElementById("saturday_tickets").value);
	var sunday_tickets = parseInt(document.getElementById("sunday_tickets").value);
	var klass_tickets = parseInt(document.getElementById("klass_tickets").value);
	var cult_tickets = parseInt(document.getElementById("cult_tickets").value);
	var pop_tickets = parseInt(document.getElementById("pop_tickets").value);
	var full_tickets = parseInt(document.getElementById("full_tickets").value);
	var red_kinderen = parseInt(document.getElementById("red_kinderen").value);

	
 	if(isNaN(movie_tickets))
	{
		/* bij niet-numerieke invoer */
		document.getElementById("err_movie_tickets").innerHTML = "Please enter a number.";
		$("#err_movie_tickets").slideDown();
		error = true;
	} else {
		if(movie_tickets >= 0)
		{
			/* bij correcte, positieve invoer */
			aantalTickets += movie_tickets;
			subtotaal += movie_tickets * prijsFilm;
			$("#err_movie_tickets").slideUp();	
		} else {
			/* bij negatieve invoer (uitgezonderd 0) */
			document.getElementById("err_movie_tickets").innerHTML = "Please enter a valid amount.";
			$("#err_movie_tickets").slideDown();
			error = true;
		}
	}
	
	
	if(isNaN(friday_tickets))
	{
		document.getElementById("err_friday_tickets").innerHTML = "Please enter a number.";
		$("#err_friday_tickets").slideDown();
		error = true;
	} else {
		if(friday_tickets >= 0)
		{
			aantalTickets += friday_tickets;
			subtotaal += friday_tickets * prijsDag;
			$("#err_friday_tickets").slideUp();		
		} else {
			document.getElementById("err_friday_tickets").innerHTML = "Please enter a valid amount.";
			$("#err_friday_tickets").slideDown();
			error = true;
		}
	}
	
	if(isNaN(saturday_tickets))
	{
		document.getElementById("err_saturday_tickets").innerHTML = "Please enter a number.";
		$("#err_saturday_tickets").slideDown();
		error = true;
	} else {
		if(saturday_tickets >= 0)
		{
			aantalTickets += saturday_tickets;
			subtotaal += saturday_tickets * prijsDag;
			$("#err_saturday_tickets").slideUp();		
		} else {
			document.getElementById("err_saturday_tickets").innerHTML = "Please enter a valid amount.";
			$("#err_saturday_tickets").slideDown();
			error = true;
		}
	}
	
	if(isNaN(sunday_tickets))
	{
		document.getElementById("err_sunday_tickets").innerHTML = "Please enter a number.";
		$("#err_sunday_tickets").slideDown();
		error = true;
	} else {
		if(sunday_tickets >= 0)
		{
			aantalTickets += sunday_tickets;
			subtotaal += sunday_tickets * prijsDag;
			$("#err_sunday_tickets").slideUp();		
		} else {
			document.getElementById("err_sunday_tickets").innerHTML = "Please enter a valid amount.";
			$("#err_sunday_tickets").slideDown();
			error = true;
		}
	}
	
	if(isNaN(klass_tickets))
	{
		document.getElementById("err_klass_tickets").innerHTML = "Please enter a number.";
		$("#err_klass_tickets").slideDown();
		error = true;
	} else {
		if(klass_tickets >= 0)
		{
			aantalTickets += klass_tickets;
			subtotaal += klass_tickets * prijsCat;
			$("#err_klass_tickets").slideUp();		
		} else {
			document.getElementById("err_klass_tickets").innerHTML = "Invalid amount!";
			$("#err_klass_tickets").slideDown();
			error = true;
		}
	}
	
	if(isNaN(cult_tickets))
	{
		document.getElementById("err_cult_tickets").innerHTML = "Please enter a number.";
		$("#err_cult_tickets").slideDown();
		error = true;
		} else {
		if(cult_tickets >= 0)
		{
			aantalTickets += cult_tickets;
			subtotaal += cult_tickets * prijsCat;
			$("#err_cult_tickets").slideUp();		
		} else {
			document.getElementById("err_cult_tickets").innerHTML = "Invalid amount!";
			$("#err_cult_tickets").slideDown();
			error = true;
		}
	}
	
	if(isNaN(pop_tickets))
	{
		document.getElementById("err_pop_tickets").innerHTML = "Please enter a number.";
		$("#err_pop_tickets").slideDown();
		error = true;
	} else {
		if(pop_tickets >= 0)
		{
			aantalTickets += pop_tickets;
			subtotaal += pop_tickets * prijsCat;
			$("#err_pop_tickets").slideUp();		
		} else {
			document.getElementById("err_pop_tickets").innerHTML = "Invalid amount!";
			$("#err_pop_tickets").slideDown();
			error = true;
		}
	}
	
	if(isNaN(full_tickets))
	{
		document.getElementById("err_full_tickets").innerHTML = "Please enter a number.";
		$("#err_full_tickets").slideDown();
		error = true;
	} else {
		if(full_tickets >= 0)
		{
			aantalTickets += full_tickets;
			subtotaal += full_tickets * prijsCat;
			$("#err_full_tickets").slideUp();		
		} else {
			document.getElementById("err_full_tickets").innerHTML = "Invalid amount!";
			$("#err_full_tickets").slideDown();
			error = true;
		}
	}

	if(isNaN(red_kinderen))
	{
		document.getElementById("err_red_kinderen").innerHTML = "Please enter a number.";
		$("#err_red_kinderen").slideDown();
		error = true;
	} else {
		if(red_kinderen >= 0)
		{
			if((aantalTickets != 0 && red_kinderen - aantalTickets >= 0) || (aantalTickets == 0 && red_kinderen > 0))
			{
				/* als er meer kindertickets dan gewone tickets worden gekocht */
				document.getElementById("err_red_kinderen").innerHTML = "Invalid amount: At least one adult ticket must be bought!";
				$("#err_red_kinderen").slideDown();
				error = true;
			} else {
				$("#err_red_kinderen").slideUp();
			}
		} else {
			document.getElementById("err_red_kinderen").innerHTML = "Invalid amount!";
			$("#err_red_kinderen").slideDown();
			error = true;
		}
	}
	
	
	
	console.log(aantalTickets);
	
	if(error || aantalTickets == 0)
	{
		$("#afrekening").slideUp();
		document.getElementsByTagName("button").item(0).disabled = true;
		document.getElementsByTagName("button").item(0).className = "disabled";

	} else {		
		$("#afrekening").slideDown();
		document.getElementsByTagName("button").item(0).disabled = false;
		document.getElementsByTagName("button").item(0).className = "enabled";
	}
	
	/* berekening (sub)totaal en korting */
	var totKorting = (subtotaal / 100) * (red_kinderen * korting);
	totKorting = Math.round(totKorting * 10) / 10
	document.getElementById("subtotaal").innerHTML = subtotaal;
	document.getElementById("kortingen").innerHTML = 0 - totKorting;
	document.getElementById("totaal").innerHTML = subtotaal - totKorting;
}

function maakMail()
{
	var subtotaal = 0;
	var movie_tickets = parseInt(document.getElementById("movie_tickets").value);
	subtotaal += movie_tickets * prijsFilm;
	var friday_tickets = parseInt(document.getElementById("friday_tickets").value);
	var saturday_tickets = parseInt(document.getElementById("saturday_tickets").value);
	var sunday_tickets = parseInt(document.getElementById("sunday_tickets").value);
	subtotaal += (friday_tickets + saturday_tickets + sunday_tickets) * prijsDag;
	var klass_tickets = parseInt(document.getElementById("klass_tickets").value);
	var cult_tickets = parseInt(document.getElementById("cult_tickets").value);
	var pop_tickets = parseInt(document.getElementById("pop_tickets").value);
	subtotaal += (klass_tickets + cult_tickets + pop_tickets) * prijsCat;
	var full_tickets = parseInt(document.getElementById("full_tickets").value);
	subtotaal += full_tickets * prijsFull;
	var red_kinderen = parseInt(document.getElementById("red_kinderen").value);
	var naam = document.getElementById("naam").value;
	var email = document.getElementById("email").value;
	var telnr = document.getElementById("telnr").value;
	var per_post = $("#per_post").prop("checked");
	
	if (per_post)
	{
		var adres_straat = document.getElementById("adres_straat").value;
		var adres_huisnr = document.getElementById("adres_huisnr").value;
		var adres_bus = document.getElementById("adres_bus").value;
		var adres_lijn2 = document.getElementById("adres_lijn2").value;
		var adres_stad = document.getElementById("adres_stad").value;
		var adres_regio = document.getElementById("adres_regio").value;
		var adres_postcode = document.getElementById("adres_postcode").value;
		var adres_land = document.getElementById("adres_land").value;
		var adres_postcode = document.getElementById("adres_postcode").value;
	}
	
	var txt = "mailto:wim.hambrouck@student.ehb.be?subject=Ticket reservation&body=";
	txt += "Movie tickets:" + movie_tickets + "%0AFriday tickets: " + friday_tickets + "%0ASaturday tickets: " + saturday_tickets + "%0ASunday tickets: " + sunday_tickets + "%0AClassics: " + klass_tickets + "%0ACult: " + cult_tickets + "%0APop: " + pop_tickets + "%0AFull: " + full_tickets + "%0A";
	
	var totKorting = (subtotaal / 100) * (red_kinderen * korting);
	totKorting = Math.round(totKorting * korting) / korting
	var totaal = subtotaal - totKorting;
	
	txt += "Total: " + totaal;

//console.log(txt);
//alert(txt);

window.open(txt);
//window.location.href(txt);
	
}



function toggleAdres()
{
	/** Als het vinkje "per post" wordt aangezet, moet het adresveld verschijnen en verplicht worden gemaakt **/
	if($("#per_post").prop("checked"))
	{
		$('.adres').slideDown();
		setVerplicht(true);
	} else {
		setVerplicht(false);
		$('.adres').slideUp();
	}
}

function setVerplicht(status)
{
	document.getElementById("adres_straat").required = status;
	document.getElementById("adres_huisnr").required = status;
	document.getElementById("adres_stad").required = status;
	document.getElementById("adres_regio").required = status;
	document.getElementById("adres_postcode").required = status;
	document.getElementById("adres_land").required = status;
}

